package com.neusoft.elm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.elm.po.Business;
import com.neusoft.elm.service.BusinessService;
import com.neusoft.elm.service.impl.BusinessServiceImpl;

public class BusinessController {
    
    public BusinessController() {
    }

    /**
     * 查询所有商家
     */
    public Object listBusiness(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("BusinessController: 查询所有商家");
        
        BusinessService service = new BusinessServiceImpl();
        List<Business> list = service.listBusiness();
        
        if (list == null) {
            System.out.println("Controller: 查询结果为null，返回空列表");
            return new ArrayList<>();
        }
        
        System.out.println("Controller: 查询到 " + list.size() + " 个商家");
        return list;
    }

    /**
     * 根据商家ID查询商家信息
     */
    public Object getBusinessById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String businessIdStr = request.getParameter("businessId");
        
        if (businessIdStr == null || businessIdStr.trim().isEmpty()) {
            System.out.println("商家ID参数为空");
            return null;
        }
        
        try {
            Integer businessId = Integer.valueOf(businessIdStr);
            BusinessService service = new BusinessServiceImpl();
            Business business = service.getBusinessById(businessId);
            
            System.out.println("Controller: 查询商家ID " + businessId + " 的信息: " + 
                             (business != null ? business.getBusinessName() : "未找到"));
            return business;
        } catch (NumberFormatException e) {
            System.out.println("商家ID格式错误: " + businessIdStr);
            return null;
        }
    }

    /**
     * 根据排序类型查询商家列表
     */
    public Object listBusinessBySort(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("BusinessController: 按排序类型查询商家");
        
        String sortType = request.getParameter("sortType");
        
        // 参数验证
        if (sortType == null || sortType.trim().isEmpty()) {
            sortType = "distance"; // 默认按距离排序
        }
        
        System.out.println("接收到排序类型: " + sortType);
        
        // 验证排序类型
        if (!isValidSortType(sortType)) {
            System.out.println("无效的排序类型: " + sortType);
            sortType = "distance";
        }
        
        BusinessService service = new BusinessServiceImpl();
        List<Business> list = service.listBusinessBySort(sortType);
        
        System.out.println("Controller: 按 " + sortType + " 排序查询到 " + list.size() + " 个商家");
        
        // 打印前几个商家信息用于调试
        for (int i = 0; i < Math.min(3, list.size()); i++) {
            Business business = list.get(i);
            System.out.println("商家" + (i+1) + ": " + business.getBusinessName() + 
                             " - 距离:" + business.getDistance() + 
                             " - 销量:" + business.getMonthlySales() + 
                             " - 评分:" + business.getRating());
        }
        
        return list;
    }
    
    /**
     * 验证排序类型是否有效
     */
    private boolean isValidSortType(String sortType) {
        return "distance".equals(sortType) || 
               "sales".equals(sortType) || 
               "rating".equals(sortType);
    }
}